package inventory.controller;

import inventory.common.CommonConstants;
import inventory.common.ResultData;
import inventory.entity.MaterialCategory;
import inventory.entity.MaterialTypical;
import inventory.service.TypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "材料类别/类型 增删查改")
@RestController
public class  TypeController {

    @Autowired
    TypeService typeService;

    @ApiOperation(value = "新建材料类别(复合接口)")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/addMaterial")
    public ResultData addMaterialCategory(MaterialCategory materialCategory,
                                          MaterialTypical materialTypical,
                                          @RequestParam("flag") String flag){
        if (flag.equals("category")) {
            typeService.addMaterialCategory(materialCategory);
        }else if (flag.equals("type")){
            typeService.addMaterialType(materialTypical);
        }
        return new ResultData("类别信息插入成功",0);
    }

    @ApiOperation(value = "根据id删除材料类别")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/deleteMaterialById")
    public ResultData deleteMaterialCategory(@RequestParam("categoryId") String categoryId,
                                             @RequestParam("typicalId") String typicalId,
                                             @RequestParam("flag") String flag){
        if (flag.equals("category")) {
            typeService.deleteMaterialCategoryById(categoryId);
        } else if (flag.equals("type")){
            typeService.deleteMaterialTypeById(typicalId);
        }
        return new ResultData("类别信息删除成功",0);
    }

    @ApiOperation(value = "根据id修改材料类别")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/modifyMaterialById")
    public ResultData modifyMaterialCategory(MaterialCategory materialCategory,
                                             MaterialTypical materialTypical,
                                             @RequestParam("flag") String flag){
        if (flag.equals("category")) {
            typeService.modifyMaterialCategoryById(materialCategory);
        }else if (flag.equals("type")){
            typeService.modifyMaterialTypeById(materialTypical);
        }
        return new ResultData("类别信息修改成功",0);
    }

    @ApiOperation(value = "查询材料类别(复合接口)")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/queryType")
    public ResultData selectMaterialCategory(@RequestParam("categoryId") String categoryId,
                                             @RequestParam("typicalId") String typicalId,
                                             @RequestParam("flag") String flag){
        if (flag.equals("category")) {
            MaterialCategory materialCategoryFind = typeService.selectMaterialCategory(categoryId);

            if (materialCategoryFind == null){
                return new ResultData("材料类别信息查询失败",-1);
            }else {
                return new ResultData("材料类别信息查询成功", 0, materialCategoryFind);
            }
        }else if (flag.equals("type")){
            MaterialTypical materialTypicalFind = typeService.selectMaterialType(typicalId);

            if (materialTypicalFind == null){
                return new ResultData("材料类型信息查询失败",-1);
            }else {
                return new ResultData("材料类型信息查询成功", 0, materialTypicalFind);
            }
        }
        return new ResultData("参数错误!",-1);
    }

    @ApiOperation(value = "查询城市")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/queryCity")
    public ResultData queryCity(){
        List list = typeService.queryCity();
        if (list == null){
            return new ResultData("城市查询失败",-1);
        } else {
            return new ResultData("城市查询成功",0,list);
        }
    }

    @ApiOperation(value = "查询县")
    @RequestMapping(CommonConstants.TYPE_PREFIX + "/queryCounty")
    public ResultData queryCounty(){
        List list = typeService.queryCounty();
        if (list == null){
            return new ResultData("县名查询失败",-1);
        } else {
            return new ResultData("县名查询成功",0,list);
        }
    }
}
